package com.test;

import java.sql.Types;
import java.util.Collections;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.test.base.BaseController;
import com.test.base.BaseEntity;

public class MybatisGenerate {

    public static void main(String[] args) {

        final String modelName = "resource";
        final String tableName = "T_RESOURCE_INFO";
        final String tableNamePrefix = "T_";
        final String outputDir = "G:\\code";

        FastAutoGenerator.create("jdbc:oracle:thin:@172.16.109.3:21521/orcl", "holiday", "holiday")
                .globalConfig(builder -> {
                    builder.author("bcc") // 设置作者
//                            .enableSwagger() // 开启 swagger 模式
                            .outputDir(outputDir); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("com.test") // 设置父包名
                            .moduleName(modelName)
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .controller("controller")
                            .xml("mapper")
                            .entity("entity")
//                            .moduleName("system") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, outputDir)); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder()
                            .enableFileOverride()
                            .superClass(BaseEntity.class)
                            .enableLombok()
                            .enableChainModel()
                            .enableRemoveIsPrefix()
                            .addSuperEntityColumns("id", "create_time", "modify_time", "deleted", "version");

                    builder.mapperBuilder()
                            .enableFileOverride()
                            .enableBaseColumnList()
                            .enableBaseResultMap();

                    builder.serviceBuilder().enableFileOverride();

                    builder.controllerBuilder().enableFileOverride().superClass(BaseController.class);

                    builder.addInclude(tableName) // 设置需要生成的表名
                            .addTablePrefix(tableNamePrefix); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

}
